<?php
	
	$tplView = new XTemplate('view/product/view.htm');
	
	$tplView->assign('baseUrl', $baseUrl);
	$productId = _POST('id');
	
	
	$sql = "SELECT * FROM tblproduct WHERE 	product_id=$productId";
	$product = $db->fetchOne($sql);
	$image 		= $db->fetchOne("SELECT img_url FROM tblproduct_img WHERE product_id =".$product['product_id']."  AND basic_img =1");
		
	$product['imgurl'] = '/media/product/images/base/no-image.png';
	if($image['img_url']){
		$product['orgin'] = $image['img_url'];
		$fileName = resizeImage( ROOT_APP.$image['img_url'], 314, 449, ROOT_APP.'/media/product/images/384x449/product-'.$product['product_id']);
		$product['imgurl'] = '/media/product/images/384x449/'.$fileName;
	}
	if($product['price']){
		$product['price'] = formatPrice($product['price']).'đ';
	}
	$tplView->assign('product', $product);
	
	$category = $db->fetchOne('SELECT * FROM tblcategory WHERE category_id='.$product['category_id']);
	$_SESSION['category_name'] = '<li><a href="'.$baseUrl.'/?c=category&a=list&id='.$product['category_id'].'">'.$category['category_name'].'</a></li> /
									<li>'.$product['product_name'].'</li>';
	//Image colors
	$sql = "SELECT * FROM tblproduct_color WHERE product_id=$productId GROUP BY color_code";
	
	$colors = $db->fetchAll($sql);
	$colorcode = _POST('colorcode');
	
	
	foreach($colors as $color){
		$tplView->insert_loop('VIEW.PINFO.PCOLOR.COLORLIST', array('COLORLIST'=>$color));
		if(!$colorcode){
			$tplView->insert_loop('VIEW.COLOR', array('COLOR'=>$color));
		}
		
	}
	if($colorcode){
		$colorLists =  $db->fetchAll("SELECT * FROM tblproduct_color WHERE product_id=$productId AND color_code = '$colorcode'");
	
		foreach($colorLists as $colorList){
			$tplView->insert_loop('VIEW.COLOR', array('COLOR'=>$colorList));	
		}
	}
	//More image
	$moreimgs = $db->fetchAll("SELECT * FROM tblproduct_img WHERE product_id=$productId AND basic_img=0 ORDER BY img_order ASC");
	foreach($moreimgs as $moreimg){
		
		$tplView->insert_loop('VIEW.MOREIMG', array('MOREIMG'=>$moreimg));
		
		
	}
	//SIMILAR
	$sql = "SELECT * FROM tblproduct WHERE product_id IN (".$product['related_product'].")";
	
	$similars = $db->fetchAll($sql);
	foreach($similars as $similar){
		if($similar['price']){
			$similar['price'] = formatPrice($similar['price']);
		}
		$image 		= $db->fetchOne("SELECT img_url FROM tblproduct_img WHERE product_id =".$similar['product_id']."  AND basic_img =1");
		
		$product['imgurl'] = '/media/product/images/base/no-image.png';
		if($image['img_url'])
			$similar['imgurl'] = $image['img_url'];
		//List color
		$colors = $db->fetchAll("SELECT * FROM tblproduct_color WHERE product_id = ".$similar['product_id'] .' GROUP BY color_code');
		foreach($colors as $color){
			$tplView->insert_loop('VIEW.PSIMILAR.SIMILAR.COLOR', array('COLOR'=>$color));
		}
		$tplView->insert_loop('VIEW.PSIMILAR.SIMILAR', array('SIMILAR'=>$similar));
	}
	$sql = "SELECT * FROM tblwebinfo WHERE 1=1";
	$contactresult = $db->fetchAll($sql);
	foreach($contactresult as $result){
		if($result['type']=='sky'){
			$tplView->assign('sky', $result['content']);
			$tplView->parse('VIEW.sky');		
		}
		if($result['type']=='yahoo'){
			$tplView->assign('yahoo', $result['content']);
			$tplView->parse('VIEW.yahoo');		
		}
	}
	if($product['description']){
		$tplView->parse('VIEW.PINFO.PDESCRIPTION');
	}
	if($product['size']){
		$tplView->parse('VIEW.PSIZE');
	}
	if(count($colors)>0){
		$tplView->parse('VIEW.PINFO.PCOLOR');
	}
	if(count($colors)>0 || $product['description']){
		$tplView->parse('VIEW.PINFO');
	}
	if(count($similars)>0){
		$tplView->parse('VIEW.PSIMILAR');
	}
	$title = $product['product_name'] .' - ';
	$tplView->parse('VIEW');
	$content = $tplView->text('VIEW');